package cn.fyg.service.finance.domain.login.impl;

import java.io.StringReader;
import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import cn.fyg.common.base.retv.Retv;
import cn.fyg.service.finance.config.Connect;
import cn.fyg.service.finance.domain.login.AccountDto;
import cn.fyg.service.finance.domain.login.LoginService;
import cn.fyg.service.finance.util.DES;
import cn.fyg.service.finance.util.GsonUtil;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.internal.Streams;
import com.google.gson.stream.JsonReader;

@Component
public class LoginServiceImpl implements LoginService{
	public static final String REST_SERVICE_URI = new Connect().getUrl();
	//public static final String REST_SERVICE_URI = "http://app.fyg.cn:9080/webdemo";
	@Override
	public Retv<List<AccountDto>> login(String user, String password) {
		// TODO Auto-generated method stub
		String result = this.loginMethod(user, password);
		if (result.contains("accounts")) {
			JsonElement je = Streams.parse(new JsonReader(new StringReader(result)));
			JsonObject jo = je.getAsJsonObject();
			//List<AccountDto> accounts = GsonUtil.parseJsonArrayWithGson(jo.get("accounts").toString(), AccountDto.class);
			List<AccountDto> accounts = GsonUtil.fromJsonList(jo.get("accounts").toString(), AccountDto.class);
			return Retv.data(accounts);
		}
		
		return Retv.error("账号或密码错误");
	}
	
	private String loginMethod(String user, String password) {
		RestTemplate restTemplate = new RestTemplate();
		String user_encrypted = "";
		String password_encrypted = "";
		try {
			user_encrypted = DES.encryptDES(user, DES.key);
			password_encrypted = DES.encryptDES(password, DES.key);
		} catch (Exception e) {
			System.out.println("error");
		}
		String result = "";
		String result_encrypted = restTemplate.postForObject(REST_SERVICE_URI + "/Login?user={user_encrypted}&password={password_encrypted}&devicetoken=111", null, String.class, user_encrypted, password_encrypted);
		try {
			result = DES.decryptDES(result_encrypted, DES.key);
		} catch (Exception e) {
			System.out.println("error");
		}
		
		/*
		String bodyValTemplate = "user=" + user + "&password=" + password + "&devicetoken=1";
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<String> entity = new HttpEntity<String>(bodyValTemplate, headers);
        restTemplate.exchange(REST_SERVICE_URI + "/Login", HttpMethod.POST, entity, String.class);
		
        restTemplate.postForObject(REST_SERVICE_URI + "/Login", entity, String.class, String.class);*/
		return result;
	}

	@Override
	public String login2(String user, String password) {
		// TODO Auto-generated method stub
		String result = this.loginMethod(user, password);
		System.out.println("result:" + result);
		
		if (result.contains("accounts")) {
			JsonElement je = Streams.parse(new JsonReader(new StringReader(result)));
			JsonObject jo = je.getAsJsonObject();
			return jo.get("accounts").toString();
		}
		
		return "error";
	}

}